package com.tac.service.impl;

import com.tac.common.result.Result;
import com.tac.common.thread.Thread;
import com.tac.dao.CategoryDao;
import com.tac.dao.ProductDao;
import com.tac.pojo.Category;
import com.tac.pojo.Product;
import com.tac.pojo.vo.Page;
import com.tac.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl  implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private ProductDao productDao;
    /**
     * 新增分类
     * @param category
     * @return
     */
    @Override
    public Result insert(Category category) {
        category.setCreateUser(Thread.getId()+"");
        category.setUpdateUser(Thread.getId()+"");
        category.setDeleteFlag(0);
        categoryDao.insert(category);
        return Result.success(null,"success");
    }

    /**
     * 删除分类 判断下面有没有商品
     * @param id
     * @return
     */
    @Override
    public Result remove(Integer id) {
        List<Product> products = productDao.selectAllByCategoryId2(id);
        if(products.size()!=0){
            return Result.error("分类下面还有商品存在不能删除");
        }
        categoryDao.remove(id);
        return  Result.success(null,"suceesss");
    }

    /**
     * 更新分类
     * @param category
     * @return
     */
    @Override
    public Result updateById(Category category) {
        category.setUpdateUser(Thread.getId()+"");
        Date date = new Date(System.currentTimeMillis());
        category.setUpdateTime(date);
        categoryDao.updateById(category);
        return Result.success(null,"success");
    }

    /**
     * 查询所有分类 分页
     * @return
     */
    @Override
    public Result<Page<List<Category>>> selectAll(int page ,int limit) {
        Integer TotalLimit = categoryDao.selectAllByCount();
        List<Category> categories = categoryDao.selectAll((page - 1) * limit, limit);
        Page<List<Category>> listPage = Page.get(categories, TotalLimit, limit);
        return Result.success(listPage,"success");
    }

    /**
     * 分级查询所有
     * @return
     */
    @Override
    public Result<List<Category>> selectAllByLevel() {
        return  Result.success(categoryDao.selectAllByLevel(0),"success");
    }


}
